home *** CD-ROM | disk | FTP | other *** search
- #include "SystemSoft.h"
- /*
- * SaveNameRegistry.c
- * Copyright © 1995, Apple Computer Inc. All Rights Reserved.
- */
-
- #include "DisplayNameRegistry.h"
-
- #define kEndOfLine 0x0D /* Return */
- #define TestTDFlag(elementHdl, mask) (((**elementHdl).flag & (mask)) != 0)
-
- /*
- * Standard File dialog items. This follows the layout of the old Standard File
- * (-3999) DITL. Our private items are at the end.
- */
- enum { /* Save File Dialog */
- kSaveButton = 1,
- kDontSaveButton,
- kSaveAsButton,
- kFolderHierarchyPopup,
- kEjectButton,
- kDriveButton,
- kFileNameText,
- kCatalogList,
- kSaveAllRadio,
- kSaveVisibleRadio,
-
- kFileCreator = 'R*ch' , // BBEdit rules
- kFileType = 'TEXT'
- };
-
- OSErr WriteVisibleNameRegistry(TwistDownHdl twistDownHandle);
- OSErr WriteEntireNameRegistry(TwistDownHdl twistDownHandle);
- OSErr WriteThisElement(TwistDownHdl twistDownHandle);
- void SaveFileDialog(short dialogID, ConstStr255Param promptString,
- ConstStr255Param originalName, SFReply * reply);
- static pascal short PutDialogFilter(short itemHit, DialogPtr theDialog);
- static void SelectDialogRadio(DialogPtr theDialog, short selectedButton);
- /* Debug only */
- void CheckForNullByte(Ptr dataPtr, long dataLength);
-
- //--------------------------------------------------------------------------------
- // Create an output file. .
- void CreateOutputFile(void)
- {
- OSErr status, closeStatus;
- TwistDownHdl listHead;
- Cell theCell;
-
- SaveFileDialog(DLOG_SFPutFile, "\pOutput File", "\pName Registry Output", &gSFReply);
- if (gSFReply.good == FALSE)
- status = userCanceledErr;
- else
- {
- // Create the file, eliminating any duplicate.
- SetCursor(*GetCursor(watchCursor));
- status = Create(gSFReply.fName, gSFReply.vRefNum, kFileCreator, kFileType);
-
- if (status == dupFNErr)
- { // Exists already?
- status = FSDelete(gSFReply.fName, gSFReply.vRefNum);
- if (status == noErr)
- status = Create(gSFReply.fName, gSFReply.vRefNum, kFileCreator, kFileType);
- }
-
- if (status == noErr)
- status = FSOpen(gSFReply.fName, gSFReply.vRefNum, &gSaveFileRefNum);
-
- if (status != noErr)
- {
- if (status != userCanceledErr)
- NonFatalError(status, "\pCan't create file");
- } else
- {
- SetPt(&theCell, 0, 0);
- listHead = GetTwistDownElementHandle(gCurrentBrowserPtr->theList, theCell);
- if (gSaveAllElements)
- status = WriteEntireNameRegistry(listHead);
- else
- status = WriteVisibleNameRegistry(listHead);
-
- if (status != noErr)
- NonFatalError(status, "\pCan't write file");
-
- closeStatus = FSClose(gSaveFileRefNum);
- if (status == noErr && closeStatus != noErr)
- {
- NonFatalError(closeStatus, "\pCan't close file");
- status = closeStatus;
- }
-
- closeStatus = FlushVol(NULL, gSFReply.vRefNum);
- if (status == noErr && closeStatus != noErr)
- NonFatalError(closeStatus, "\pCan't flush volume");
- }
- InitCursor();
- }
- }
-
- #ifdef SYSF
-
- #include <stdio.h>
- #include <Strings.h>
-
- #define FILENAMESTUB "NameRegSnap"
-
- // this a Real Hack
- Boolean gIgnoreUntilFirstCardEntry;
-
- //--------------------------------------------------------------------------------
- // find an unused filename and create a text file that will be used in the snapshot
- // the file will be created in the current directory
- OSErr GetOutFileName(Str255 outFileName);
- OSErr GetOutFileName(Str255 outFileName)
- {
- OSErr status = dupFNErr;
- short vRefNum = 0;
- static short uniqueID = 0;
-
- while (status != noErr)
- {
- sprintf((char *) outFileName, "%s%d", FILENAMESTUB, uniqueID);
- c2pstr((char *) outFileName);
-
- status = Create(outFileName, vRefNum, kFileCreator, kFileType);
- uniqueID++;
- }
-
- return status;
- }
-
-
- //--------------------------------------------------------------------------------
- // Write a snapshot of the current state of the registry window to a disk file
- void SnapshotToDisk(Boolean isExpanded, Boolean noSockets, char *fileName)
- {
- OSErr status = noErr, ignore, closeStatus;
- TwistDownHdl listHead;
- Cell theCell;
- Str255 outFileName;
- short vRefNum = 0;
-
- // open a file
- if (strlen(fileName) == 0)
- status = GetOutFileName(outFileName);
- else
- {
- strcpy((char *) outFileName, fileName);
- c2pstr((char *) outFileName);
- ignore = FSDelete(outFileName,vRefNum);
- ignore = Create(outFileName, vRefNum, kFileCreator, kFileType);
- }
-
- if (status == noErr)
- status = FSOpen(outFileName, vRefNum, &gSaveFileRefNum);
-
- // set a global Boolean - HACK
- gIgnoreUntilFirstCardEntry = noSockets;
-
- // scan the list and write out the registry entries
- SetPt(&theCell, 0, 0);
- listHead = GetTwistDownElementHandle(gCurrentBrowserPtr->theList, theCell);
- if (isExpanded)
- status = WriteEntireNameRegistry(listHead);
- else
- status = WriteVisibleNameRegistry(listHead);
-
- if (status != noErr)
- NonFatalError(status, "\pCan't write file");
-
- // all done, close the file
- closeStatus = FSClose(gSaveFileRefNum);
- if (status == noErr && closeStatus != noErr)
- {
- NonFatalError(closeStatus, "\pCan't close file");
- status = closeStatus;
- }
-
- // all done, really make sure that it is closed
- closeStatus = FlushVol(NULL, vRefNum);
- if (status == noErr && closeStatus != noErr)
- NonFatalError(closeStatus, "\pCan't flush volume");
- }
- #endif
-
- //--------------------------------------------------------------------------------
- OSErr WriteVisibleNameRegistry(TwistDownHdl twistDownHandle)
- {
- OSErr status;
-
- status = noErr;
- while (status == noErr && twistDownHandle != NULL)
- {
- status = WriteThisElement(twistDownHandle);
- if (status == noErr
- && (**twistDownHandle).subElement != NULL
- && TestTDFlag(twistDownHandle, kShowSublist))
- status = WriteVisibleNameRegistry((**twistDownHandle).subElement);
- twistDownHandle = (**twistDownHandle).nextElement;
- }
- return (status);
- }
-
- //--------------------------------------------------------------------------------
- OSErr WriteEntireNameRegistry(TwistDownHdl twistDownHandle)
- {
- OSErr status;
-
- status = noErr;
- while (status == noErr && twistDownHandle != NULL)
- {
- status = WriteThisElement(twistDownHandle);
- if (status == noErr && (**twistDownHandle).subElement != NULL)
- status = WriteEntireNameRegistry((**twistDownHandle).subElement);
- twistDownHandle = (**twistDownHandle).nextElement;
- }
- return (status);
- }
-
- //--------------------------------------------------------------------------------
- // Write one line of text to the output file.
- OSErr WriteThisElement(TwistDownHdl twistDownHdl)
- {
- OSErr status;
- long textLength = 1;
- short handleState;
- static char gEndOfLine[1] = { kEndOfLine };
- #define ELEM (**twistDownHdl)
-
- handleState = HGetState((Handle) twistDownHdl);
- HLock((Handle) twistDownHdl);
- #ifdef SYSF
- if (gIgnoreUntilFirstCardEntry)
- {
- if (strncmp(WECARECARD, (const char *) ELEM.data, strlen(WECARECARD)) != 0) // "Devices:device-tree:bandit:ti1130:"
- return (noErr);
- // we have skipped all the sockets now start listing elements
- gIgnoreUntilFirstCardEntry = false;
- }
-
- // put a blank line before every top level registry entry
- if (ELEM.indentLevel == 0)
- status = FSWrite(gSaveFileRefNum, &textLength, gEndOfLine);
- #endif
- textLength = ELEM.dataLength;
- if (textLength == 0)
- status = noErr;
- else {
- CheckForNullByte((Ptr) ELEM.data, ELEM.dataLength);
- status = FSWrite(gSaveFileRefNum, &textLength, ELEM.data);
- }
- HSetState((Handle) twistDownHdl, handleState);
- if (status == noErr) {
- textLength = 1;
- status = FSWrite(gSaveFileRefNum, &textLength, gEndOfLine);
- }
- if (status != noErr)
- NonFatalError(status, "\pWriteThisElement");
- return (status);
- }
-
- //--------------------------------------------------------------------------------
- void SaveFileDialog(short dialogID, ConstStr255Param promptString,
- ConstStr255Param originalName, SFReply * reply)
- {
- static DlgHookUPP dlgHookUPP = NULL;
- Point where;
-
- SetPt(&where, 80, 80);
- if (dlgHookUPP == NULL)
- dlgHookUPP = NewDlgHookProc(PutDialogFilter);
- SFPPutFile(
- where,
- promptString,
- originalName,
- dlgHookUPP,
- reply,
- dialogID, NULL);
- }
-
- //--------------------------------------------------------------------------------
- static pascal short PutDialogFilter(short itemHit, DialogPtr theDialog)
- {
- short itemType;
- Handle itemHandle;
- Rect itemRect;
-
- switch (itemHit)
- {
- case sfHookFirstCall:
- SelectDialogRadio(theDialog, (gSaveAllElements) ? kSaveAllRadio : kSaveVisibleRadio);
- break;
-
- case kSaveAllRadio:
- case kSaveVisibleRadio:
- SelectDialogRadio(theDialog, itemHit);
- break;
-
- case kSaveButton:
- GetDialogItem(theDialog, kSaveAllRadio, &itemType, &itemHandle, &itemRect);
- gSaveAllElements = GetControlValue((ControlHandle) itemHandle);
- }
- return (itemHit);
- }
-
-
- //--------------------------------------------------------------------------------
- static void SelectDialogRadio(DialogPtr theDialog, short selectedButton)
- {
- short itemType, i;
- Handle itemHandle;
- Rect itemRect;
-
- for (i = kSaveAllRadio; i <= kSaveVisibleRadio; i++)
- {
- GetDialogItem(theDialog, i, &itemType, &itemHandle, &itemRect);
- SetControlValue((ControlHandle) itemHandle, (i == selectedButton) ? 1 : 0);
- }
- }
-
- //--------------------------------------------------------------------------------
- void CheckForNullByte(Ptr dataPtr, long dataLength)
- {
- long i;
-
- for (i = 0; i < dataLength; i++)
- {
- if (dataPtr[i] == 0)
- {
- printf("%ld in %ld \"%.*s\"\n", i, dataLength, dataLength, dataPtr);
- break;
- }
- }
- }
-
-